package org.szy.plugin.sys.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.mybatisflex.core.paginate.Page;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.ModelAndView;
import org.noear.solon.validation.annotation.NoRepeatSubmit;
import org.noear.solon.validation.annotation.NotBlank;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Validated;
import org.szy.core.common.controller.BaseController;
import org.szy.core.common.entity.R;
import org.szy.core.common.param.CommonStatusParam;
import org.szy.plugin.sys.entity.SysUser;
import org.szy.plugin.sys.param.SysUserQueryParam;
import org.szy.plugin.sys.service.SysUserService;

/**
 * 系统用户表控制器
 *
 * @author wangbuhao
 * @since 2023-06-27 09:56:03
 */
@Controller
@Mapping("/sys/user")
public class SysUserController extends BaseController<SysUser> {

    private final String PATH_PREFIX = "sys/user/";
    @Inject
    private SysUserService sysUserService;

    /**
     * 系统用户表列表页面
     */
    @Get
    @Mapping("/index")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "index");
        return view;
    }

    /**
     * 系统用户表编辑页
     */
    @Get
    @Mapping("/edit")
    public ModelAndView edit() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "edit");
        return view;
    }

    /**
     * 系统用户表详情页
     */
    @Get
    @Mapping("/detail")
    public ModelAndView detail() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "detail");
        return view;
    }

    /**
     * 选择用户页面
     */
    @Get
    @Mapping("/sel")
    public ModelAndView sel() {
        return new ModelAndView(PATH_PREFIX + "sel");
    }

    /**
     * 获取系统用户表分页数据
     *
     * @author wangbuhao
     * @since 2023-06-27 09:56:03
     */
    @Get
    @Mapping("/page")
    @SaCheckPermission(value = "sys_user_list", mode = SaMode.OR, orRole = "wangbuhao")
    public R<Page<SysUser>> page(SysUserQueryParam param) {
        return R.data(sysUserService.page(getPage(), param));
    }

    /**
     * 获取系统用户表详情
     *
     * @author wangbuhao
     * @since 2023-06-27 09:56:03
     */
    @Get
    @Mapping("/getInfo")
    @SaCheckPermission(value = "sys_user_detail", mode = SaMode.OR, orRole = "wangbuhao")
    public R<SysUser> getInfo(@NotNull Long id) {
        return R.asNull(sysUserService.getById(id));
    }

    /**
     * 添加系统用户表
     *
     * @author wangbuhao
     * @since 2023-06-27 09:56:03
     */
    @Post
    @Mapping("/add")
    @NoRepeatSubmit
    @SaCheckPermission(value = "sys_user_add", mode = SaMode.OR, orRole = "wangbuhao")
    public R<?> add(@Validated SysUser entity) {
        boolean save = sysUserService.add(entity);
        return save ? R.ok("保存成功", entity.getId()) : R.fail("保存失败");
    }

    /**
     * 编辑系统用户表
     *
     * @author wangbuhao
     * @since 2023-06-27 09:56:03
     */
    @Put
    @Mapping("/update")
    @NoRepeatSubmit
    @SaCheckPermission(value = "sys_user_update", mode = SaMode.OR, orRole = "wangbuhao")
    public R<?> update(@Validated SysUser entity) {
        boolean update = sysUserService.updateById(entity);
        return R.bool(update);
    }

    /**
     * 删除系统用户表
     *
     * @author wangbuhao
     * @since 2023-06-27 09:56:03
     */
    @Delete
    @Mapping("/delete")
    @SaCheckPermission(value = "sys_user_delete", mode = SaMode.OR, orRole = "wangbuhao")
    public R<?> delete(@NotNull Long id) {
        boolean delete = sysUserService.removeById(id);
        return R.bool(delete, "删除成功", "删除失败，数据可能正在使用或已删除");
    }

    /**
     * 修改系统用户表状态
     *
     * @param param 公共状态参数
     * @return 修改结果
     */
    @Put
    @Mapping("/modifyStatus")
    @SaCheckPermission(value = "sys_user_update", mode = SaMode.OR, orRole = "wangbuhao")
    public R<?> modifyStatus(CommonStatusParam param) {
        boolean status = sysUserService.modifyStatus(param);
        return R.bool(status);
    }

    /**
     * 修改密码
     *
     * @author wangbuhao
     * @date 2023年2月13日21:02:00
     */
    @Put
    @Mapping("/modifyPwd")
    @SaCheckPermission(value = "sys_user_modifyPwd", mode = SaMode.OR, orRole = "wangbuhao")
    public R<?> modifyPwd(@NotNull Long id, @NotBlank(message = "密码不能为空") String password) {
        boolean modify = sysUserService.modifyPwd(id, password);
        return R.bool(modify);
    }

}
